# -*- coding: utf-8 -*-
"""
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

from resource_management import *
from resource_management.core.resources.system import Execute
from resource_management.libraries.functions.check_process_status import check_process_status
from resource_management.libraries.script.script import Script
from resource_management.core.logger import Logger
from resource_management.core.exceptions import ClientComponentHasNoStatus
from resource_management.core.resources.system import Directory,File
from presto_env import presto_env
import time



class Presto_Client(Script):
    def install(self, env):
        import params
        env.set_params(params)

        # 删除现有软链接
        Execute('rm -f /usr/local/bin/presto')

        # 删除现有presto客户端
        Execute('rm -rf {0}/presto-cli*'.format(params.presto_home))

        Directory([params.presto_home],
                  mode=0755,
                  cd_access='a',
                  owner=params.presto_user,
                  group=params.presto_group,
                  create_parents=True
                  )

        # Download presto.tar.gz
        Execute('wget {0} -O presto-cli.jar'.format(params.presto_client_download_url))

        Execute('mv presto-cli.jar {0}/'.format(params.presto_home))

        Execute('chmod +x {0}/presto-cli.jar'.format(params.presto_home))

        Execute('ln -s {0}/presto-cli.jar /usr/local/bin/presto'.format(params.presto_home))

        Logger.info("Install complete")

    def configure(self, env):
        import params
        env.set_params(params)
        print("It just a client")

    def status(self, env):
        raise ClientComponentHasNoStatus()

if __name__ == "__main__":
  Presto_Client().execute()
